<template>
  <div>
    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <!-- 面包屑导航区 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>留言管理</el-breadcrumb-item>
        <el-breadcrumb-item>新闻稿管理</el-breadcrumb-item>
      </el-breadcrumb>

      <!-- 搜索区 -->
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input
            placeholder="按标题搜索新闻稿"
            v-model="queryInfo.query"
            clearable>
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="searchJournalism">
            </el-button>
          </el-input>
        </el-col>

        <el-col :span="3">
          <el-dropdown>
            <span class="el-dropdown-link">
              显示新闻稿<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="displayAll">显示全部新闻稿</el-dropdown-item>
              <el-dropdown-item @click.native="displayAudited">显示已审核新闻稿</el-dropdown-item>
              <el-dropdown-item @click.native="displayUnaudited">显示未审核新闻稿</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>

        <el-col :span="2">
          <el-button type="warning" @click="batchRevers">反选</el-button>
        </el-col>
        
        <el-col :span="2">
          <el-button type="danger" @click="batchDelJournalism">批量删除</el-button>
        </el-col>
      </el-row>

      <!-- 新闻稿表格区 -->
      <el-table
        :header-cell-style="{textAlign: 'center'}"
        :cell-style="{ textAlign: 'center' }"
        :data="tableData"
        @selection-change="handleSelectionChange"
        border
        style="width: 100%">
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          prop="id"
          label="序号"
          width="100">
        </el-table-column>
        <el-table-column
          prop="author"
          label="作者"
          width="180">
        </el-table-column>
        <el-table-column
          label="发布时间">
          <template slot-scope="scope">
            {{scope.row.time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column
          label="标题"
          prop="title">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button 
              type="primary" 
              icon="el-icon-edit" 
              size="mini"
              @click="editJournalism(scope.row)"></el-button>
            <el-button 
              type="danger" 
              icon="el-icon-delete" 
              size="mini"
              @click="delJournalism(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background>
      </el-pagination>
    </el-card>
    
  </div>
</template>

<script>
export default {
  created() {
    this.getAllJournalism()
  },
  data() {
    return {
      queryInfo: {
        pageNum: 1,
        pageSize: 10,
        query: ""
      },
      tableData: [],
      total: 0,
      opeDialogVisible: false,
      ids: []
    }
  },
  methods: {
    // 获取全部新闻稿
    async getAllJournalism() {
      const { data: res } = await this.$http.post('getAllJournalism', this.queryInfo)
      if (res.code != 'success') return this.$message.error('新闻稿获取失败')
      this.tableData = res.data
      this.total = res.numbers
      this.$message.success('新闻稿获取成功')
    },
    // 修改按钮被点击时，路由跳转，并在路径上附加被点击文章的 id
    editJournalism(journalism) {
      this.$router.push(`alter?id=${journalism.id}`)
    },
    // 查询新闻稿
    async searchJournalism() {
      const { data: res } = await this.$http.post('getAllJournalism', this.queryInfo)
      if (res.code != 'success') return this.$message.error('新闻稿查询失败')
      this.tableData = res.data
      this.total = res.numbers
      this.$message.success('新闻稿查询成功')
    },
    // 单个删除
    async delJournalism(id) {
      const confirmResult = await this.$confirm('此操作将永久删除该新闻稿, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') return this.$message.info('已取消删除')
      const { data: res } = await this.$http.post('batchDeleteJournalism', { ids: id })
      if (res != 'success') return this.$message.error('新闻稿删除失败')
      this.$message.success('新闻稿删除成功')
      this.getAllJournalism()
    },
    // 批量删除
    async batchDelJournalism() {
      const confirmResult = await this.$confirm('此操作将永久删除这些新闻稿, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') return this.$message.info('已取消删除')
      const { data: res } = await this.$http.post('batchDeleteJournalism', { ids: this.ids.join(',') })
      if (res != 'success') return this.$message.error('新闻稿删除失败')
      this.$message.success('新闻稿删除成功')
      this.getAllJournalism()
    },
    // 批量显示
    handleSelectionChange(obj) {
      this.ids = []
      obj.forEach(item => this.ids.push(item.id))
    },
    // 显示全部
    displayAll() {},
    // 显示已审核
    displayAudited() {},
    // 显示未审核
    displayUnaudited() {},
    // 反选
    batchRevers() {},
    handleSizeChange(size) {
      this.queryInfo.pageSize = size
      this.getAllJournalism()
    },
    handleCurrentChange(num) {
      this.queryInfo.pageNum = num
      this.getAllJournalism()
    },
  }
}
</script>

<style scoped>
.el-icon-arrow-down {
  margin-top: 12px;
}
</style>